#include <stdlib.h>
#include <string.h>

//person 类的公共接口
#include "ExtremeC_examples_chapter8_3_person.h"

//前置声明
typedef struct {
    char* student_number;
    unsigned int passed_credits;
    //我们必须在这里使用一个指针，因为 person_t 类型不是完整的
    struct person_t* person;
} student_t;

//内存分配函数
student_t* student_new() {
    return (student_t*)malloc(sizeof(student_t));
}

//构造函数
void student_ctor(student_t* student, const char* first_name,
                  const char* last_name, unsigned int birth_year,
                  const char* student_number, unsigned int passed_credits) {
    //为父对象分配内存
    student->person = person_new();
    person_ctor(student->person, first_name, last_name, birth_year);
    student->student_number = (char*)malloc(16 * sizeof(char));
    strcpy(student->student_number, student_number);
    student->passed_credits = passed_credits;
}

//析构函数
void student_dtor(student_t* student) {
    //先析构子对象
    free(student->student_number);
    //然后调用父类的析构函数
    person_dtor(student->person);
    //释放父对象分配的内存
    free(student->person);
}

//行为函数
void student_get_first_name(student_t* student, char* buffer) {
    //我们必须使用 person 的行为函数
    person_get_first_name(student->person, buffer);
}

void student_get_last_name(student_t* student, char* buffer) {
    //我们必须使用 person 的行为函数
    person_get_last_name(student->person, buffer);
}

unsigned int student_get_birth_year(student_t* student) {
    //我们必须使用 person 的行为函数
    return person_get_birth_year(student->person);
}
void student_get_student_number(student_t* student, char* buffer) {
    strcpy(buffer, student->student_number);
}

unsigned int student_get_passed_creadits(student_t* student) {
    return student->passed_credits;
} 